package com.partner.action.product;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.partner.bean.product.Product;
import com.partner.frame.Result;
import com.partner.frame.common.ConstantsMark;
import com.partner.service.product.ProductService;

@Controller
@RequestMapping("/product")
public class ProductAction {

	@Autowired
	private ProductService productService;
	
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request,Product product
			) throws ParseException{
		ModelAndView result = new ModelAndView();
		List<Product> productList = productService.selectAll(product);
		result.addObject("product", product);
		result.addObject("productList", productList);
		result.setViewName("/pages/product/list");
		return result;
	}
	
	@RequestMapping(value = "/before",method = RequestMethod.GET)
	public ModelAndView before(HttpServletRequest request,HttpServletResponse response,Integer id){

		ModelAndView result = new ModelAndView();
		Product data = new Product();
		if(id!=0){
			data = productService.selectByPrimaryId(id);
		}
		result.addObject("data", data);
		result.setViewName("/pages/product/edit");
		return result;
	}
	
	@RequestMapping(value = "/updateProduct",method = RequestMethod.POST)
	@ResponseBody
	public Result updateProduct(HttpServletRequest request,Product product) throws ParseException{
		Result result = new Result();
		//id == 0 进入新增
		if(product.getId()==0){
			productService.insert(product);
			result.setDesc(ConstantsMark.INFO_SAVESUCCESS);
		}else{
			//进入修改
			productService.updateByPrimaryKey(product);
			result.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
		}
		return result;
	}
	
	@RequestMapping(value = "/deleteProductBatch",method = RequestMethod.POST)
	@ResponseBody
	public Result deleteProductBatch(HttpServletRequest request,HttpServletResponse response,String param){
		Result result = new Result();
		String[] ids = param.split(",");
		if(ids.length>0){
			productService.deleteByids(ids);
		}
		result.setDesc(ConstantsMark.INFO_DELETESUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/deleteProduct",method = RequestMethod.POST)
	@ResponseBody
	public Result deleteProduct(HttpServletRequest request,HttpServletResponse response,String id){
		Result result = new Result();
		if(!StringUtils.isEmpty(id)){
			productService.removeByPrimaryKey(Integer.parseInt(id));
		}
		result.setDesc(ConstantsMark.INFO_DELETESUCCESS);
		return result;
	}
}
